package Compare;

import java.util.Arrays;
import java.util.Comparator;

class Stu{
    String name;
    int age;
    public Stu(String name,int age){
        this.name = name;
        this.age = age;
    }
}
class Compare implements Comparator<Stu>{

    @Override
    public int compare(Stu o1, Stu o2) {
        return o1.age - o2.age;
    }
}
public class Test01 {
    public static void main1(String[] args) {
        Stu stu1 = new Stu("zs",20);
        Stu stu2 = new Stu("ls",18);
        Compare compare = new Compare();
        System.out.println(compare.compare(stu1, stu2));
    }

    public static void main2(String[] args) {
        int[][] arr = new int[][]{{1,3},{2,4},{6,9},{5,7}};
        int n = arr.length;
        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        for(int i = 0;i<n;i++){
            for(int j = 0;j<2;j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }


    }

//    public static void main(String[] args) {
//        int[] arr = new int[]{1,3,5,2,4,6};
//        Arrays.sort(arr, new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return 0;
//            }
//
//        });
//    }
//public static void main(String[] args) {
//    Integer[] arr = {-1,3,-5,2,4,-6};
////    Arrays.sort(arr, new Comparator<Integer>()
//    Arrays.sort(arr, new Comparator<Integer>(){
//        @Override
//        public int compare(Integer o1, Integer o2) {
//            // 按从大到小的规则排序
//            return o2 - o1;
//        }
//    });
//}
public static void main3(String[] args) {
    //数组里面元素的类型是包装类,即引用数据类型
    //因为比较器就是通过引用类型进行比较的
    Integer[] arr = {1, 3, 2, 8, 5, 7};
    // 使用自定义的 Comparator 进行排序
    Arrays.sort(arr, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            // 按从大到小的规则排序
            return o2 - o1;
        }
    });

    System.out.println(Arrays.toString(arr));  // 输出：[8, 7, 5, 3, 2, 1]
}

    public static void main(String[] args) {
        int[][] arr = new int[][]{{1,3},{2,4},{6,9},{5,7}};
        Arrays.sort(arr,Comparator.comparingInt(t->-t[0]));
        int n = arr.length;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<2;j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }

}
